package com.ruoyi.system.order;
import com.alibaba.fastjson2.JSON;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: liuzhiping
 * @date: 2024/10/7 15:50
 **/
public class WeiXinUtil {


    //订单号
    public static final String ORDER_ID = "30241007666888005";

    /***
     * 下单接口
     * @return
     */
    public static String payOrder(String orderId) throws  Exception{

        Map<String,String> map = new HashMap<String,String>();

        map.put("appid",Constants.APP_ID);
        map.put("mch_id",Constants.MCH_ID);



        map.put("nonce_str",CommUtil.getNonce_str());
        map.put("body","专高6测试商品2");


        //map.put("out_trade_no",WeiXinUtil.ORDER_ID);

        map.put("out_trade_no",orderId);


        map.put("total_fee","1");//分
        map.put("spbill_create_ip","127.0.0.1");
        map.put("notify_url",Constants.NOTIFY_URL);//公网地址

        map.put("trade_type","NATIVE");

        map.put("sign_type","HMAC-SHA256");


        map.put("sign",CommUtil.generateSignature(map, Constants.API_KEY, "HMACSHA256"));


        String reqXml = XMLUtil.mapToXml(map);


        String respXml = HttpUtil.postData(Constants.UFDODER_URL,reqXml);
        System.out.println(respXml);



        Map<String,String> respMap = XMLUtil.doXMLParse(respXml);

        String codeUrl = respMap.get("code_url");


        System.out.println(codeUrl);

        return codeUrl;

    }

    /**
     *
     * @return
     */
    public static String queryOrder(String orderId) throws Exception{
        //String orderId = WeiXinUtil.ORDER_ID;
        Map<String,String> map = new HashMap<String,String>();

        map.put("appid",Constants.APP_ID);
        map.put("mch_id",Constants.MCH_ID);
        map.put("nonce_str",CommUtil.getNonce_str());
        map.put("out_trade_no",orderId);

        map.put("trade_type","NATIVE");

        map.put("sign_type","HMAC-SHA256");


        map.put("sign",CommUtil.generateSignature(map, Constants.API_KEY, "HMACSHA256"));


        String reqXml = XMLUtil.mapToXml(map);


        String respXml = HttpUtil.postData(Constants.CHECK_URL,reqXml);
        System.out.println(respXml);



        Map<String,String> respMap = XMLUtil.doXMLParse(respXml);


        return JSON.toJSONString(respMap);


    }




    public static void main(String[] args) throws  Exception {
        //WeiXinUtil.payOrder();
        //WeiXinUtil.queryOrder();
    }
}
